package com.app.qbake.util;

import java.text.DecimalFormat;

public class FormatUtil {

    public static String formatMoney(double money){
       return formatMoney(money,false);
    }
    public static String formatMoney(double money,boolean removeOdd){
        DecimalFormat format=new DecimalFormat("#######0.0");
        String formatMoney= format.format(money);
        if (removeOdd){
            formatMoney=removeOdd(formatMoney);
        }
        return formatMoney;
    }
    /**
     * 小数点后一位数字<5直接抹去，>5则变成5
     * @param price
     * @return
     */
    public static String removeOdd(String price) {
        String decimalStr=price.split("\\.")[1];
        int decimal=Integer.parseInt(decimalStr);
        StringBuilder amountBuilder=new StringBuilder();
        amountBuilder.append(price.split("\\.")[0])
                .append(".");
        int eraseValue;
        if (decimal>=5){
            amountBuilder.append("5");
            eraseValue=decimal-5;
        }else {
            amountBuilder.append("0");
            eraseValue=decimal;
        }


        return amountBuilder.toString();
    }
}
